/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package customGuiControl.Listner;

import customGuiControl.ImagePanel;
import customGuiControl.TextPane;
import customGuiControl.roundButton;
import customGuiControl.roundedPanel;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import presentation.proportional.gui;

/**
 *
 * @author emilio
 */
public final class buttonRollOverListner implements MouseListener {

    private Color prevStartColor=null;
    private Color prevEndColor=null;
    private Color prevStrokeColor=null;
    private Color prevPressdShadowColor=null;
    private Color shadowColorPressed=null;
    private Color shadowCorolOver=null;
    private Color prevShadowColorOver;
    private float strokeWidth;
    private float prevStrokeWidth;
    private boolean boolPressed=false;
    private ImagePanel panel;
    private String pathImg;
    private String pressedPathImg;
    private int ColorBrightnessPercent=15;
    private JFrame frameParent=null;
    private Cursor defCursor=null;
    private TextPane infoTextPane;
    private String infoText;
    private String previousInfoText;

    public buttonRollOverListner()
    {

    }
    public buttonRollOverListner(Color shadowColorPressed,Color shadowCorolOver,
                                 float strokeWidth, String pathImg )
    {
        this.shadowColorPressed=shadowColorPressed;
        this.shadowCorolOver=shadowCorolOver;
        this.strokeWidth=strokeWidth;
        this.pressedPathImg=pathImg;
    }

    public buttonRollOverListner(Color shadowColorPressed,Color shadowCorolOver,
                                 float strokeWidth, String pathImg, gui parent, String infoText) {

        this(shadowColorPressed, shadowCorolOver, strokeWidth, pathImg);
        this.setFrameParent(parent);
        this.infoText=infoText;
    }
    public void mouseClicked(MouseEvent e) {
        try{
            ((gui)this.getFrameParent()).buttonClicked(((roundButton)e.getSource()).getName());
        }
        catch(Exception exp){
            
        }
    }

    public void mousePressed(MouseEvent e) {
        if(e.getSource().getClass().toString().equals(roundedPanel.class.toString()));//????????????????????????
        roundButton target=(roundButton)e.getSource();
        target.setStrokeWidth(this.strokeWidth);
        this.prevPressdShadowColor=target.getShadowColor();
        target.setShadowColor(this.shadowColorPressed);
        target.setGradient(this.brighter(this.prevEndColor, this.ColorBrightnessPercent),
                           this.brighter(this.prevStartColor, this.ColorBrightnessPercent),
                           target.getGradientPercent());
        this.panel.setPathImg(this.pressedPathImg);
        target.dirtyRepaint();
    }

    public void mouseReleased(MouseEvent e) {
        if (this.boolPressed)
        {
            roundButton target=(roundButton)e.getSource();
            target.toogleSelected();
            if((!target.getSelected() || target.getRoot()==null))
            {
                target.setShadowColor(this.prevPressdShadowColor);
                target.setStrokeWidth(this.prevStrokeWidth);
                target.setGradient(this.brighter(this.prevStartColor, this.ColorBrightnessPercent),
                                   this.brighter(this.prevEndColor, this.ColorBrightnessPercent),
                                   target.getGradientPercent());
                panel.setPathImg(this.pathImg);
                target.dirtyRepaint();
            }else
            target.dirtyRepaint();
        }
    }

    public void mouseEntered(MouseEvent e) {
        this.changeCursor();
        roundButton target=(roundButton)e.getSource();
        this.boolPressed=true;
        this.prevStartColor=target.getGradient().get(0);
        this.prevEndColor=target.getGradient().get(1);
        this.prevStrokeColor=target.getStrokeColor();
        this.prevStrokeWidth=target.getStrokeWidth();
        this.prevShadowColorOver=target.getShadowColor();
        this.panel=(ImagePanel)target.getComponent(0);
        this.pathImg = panel.getPathImg();
        
        target.setShadowColor(this.shadowCorolOver.brighter());
        
        Color prevBrighterSta=this.brighter(this.prevStartColor, this.ColorBrightnessPercent);
        Color prevBrighterEnd=this.brighter(this.prevEndColor, this.ColorBrightnessPercent);
        target.setGradient(prevBrighterSta,prevBrighterEnd,target.getGradientPercent());
        target.dirtyRepaint();

        this.infoTextPane=((gui)this.getFrameParent()).getInfoTextPane();
        this.previousInfoText=this.infoTextPane.getText();
        this.infoTextPane.setText(this.infoText);
        this.infoTextPane.setBold(true);
    }

    public void mouseExited(MouseEvent e) {
        this.changeCursor();
        this.boolPressed=false;
        roundButton target=(roundButton)e.getSource();
        target.setShadowColor(this.prevShadowColorOver);
        target.setStrokeWidth(this.prevStrokeWidth);
        target.setGradient(this.prevStartColor,this.prevEndColor,target.getGradientPercent());
        panel.setPathImg(this.pathImg);
        target.dirtyRepaint();

        this.infoTextPane.setText(this.previousInfoText);
        this.infoTextPane.setBold(false);
    }

    public Color brighter(Color c,int percentLum)
    {
        int redSta=((c.getRed()+(c.getRed()*percentLum/100))<255)?(c.getRed()+(c.getRed()*percentLum/100)): 255;
        int greenSta=((c.getGreen()+(c.getGreen()*percentLum/100))<255)?(c.getGreen()+(c.getGreen()*percentLum/100)): 255;
        int blueSta=c.getBlue();
        return new Color(redSta,greenSta,blueSta);
    }

    public int getColorBrightnessPercent() {
        return ColorBrightnessPercent;
    }

    public void setColorBrightnessPercent(int ColorBrightnessPercent) {
        this.ColorBrightnessPercent = ColorBrightnessPercent;
    }

    public JFrame getFrameParent() {
        return frameParent;
    }

    public void setFrameParent(JFrame frameParent) {
        this.frameParent = frameParent;
    }

    public void changeCursor()
    {
        if(this.frameParent!=null)
        {
            
            if(this.defCursor==null)
            {
                this.defCursor=this.frameParent.getCursor();
                this.frameParent.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            else
            {
                this.frameParent.setCursor(this.defCursor);
                this.defCursor=null;
            }
        }
    } 
}
